package yxy.game.pm2.module.shop.handle;

import org.joda.time.DateTime;

import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.magicshop;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.PlayerBean;
import yxy.game.pm2.bean.currency.CurrencyNotEnoughException;
import yxy.game.pm2.bean.db.dao.MySQLServerDAO;
import yxy.game.pm2.bean.db.module.server;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.message.ShopMessage;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.player.FunctionHandle;
import yxy.game.pm2.module.shop.Goods;
import yxy.game.pm2.module.shop.GoodsLimitType;
import yxy.game.pm2.module.shop.StrategyShopModule;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/** 商店 兵法商店 */
public class StrategyShopHandle extends ShopHandle<magicshop> {

	public void packResponse(String playerCode, ShopMessage.StrategyShopResponse.Builder response) {
		StrategyShopModule module = getModule(playerCode);
		module.canRefresh();
//		response.addAllGoods(goods(playerCode));
		response.setResetCost(new CurrencyHandle().packPoint(StrategyShopModule.ShopRefreshCost()));
		response.setResetFree(module.getFreeTimes());
		response.setResetFreeTime(module.refreshTimesFreeTime());
		response.setResetTimes(module.getResetTimes());
	}

	/** 手动刷新 */
	public Result refresh(String playerCode, ShopMessage.StrategyShopUpdateResponse.Builder response) {
		StrategyShopModule module = StrategyShopModule.get(playerCode);
		if (!module.canRefresh()) {
			return Result.ShopCannotRefresh;
		}
		if (module.refreshFree()) {
			packRefresh(playerCode, module, response);
			return Result.Success;
		}
		Currency cost = StrategyShopModule.ShopRefreshCost();// 刷新消费
		try {
			new CurrencyHandle().cost(playerCode, CCReason.商店, cost);
		} catch (CurrencyNotEnoughException e) {
			return new Result(Result.CostFail, e.getType().getNumber(), e.getDataId());// 消费不足
		}
		module.refresh();
		packRefresh(playerCode, module, response);
		return Result.Success;
	}

	private void packRefresh(String playerCode, StrategyShopModule module, ShopMessage.StrategyShopUpdateResponse.Builder response) {
		response.addAllGoods(checkShopItemIsShow(playerCode));
		response.setResetCost(new CurrencyHandle().packPoint(StrategyShopModule.ShopRefreshCost()));
		response.setResetFree(module.getFreeTimes());
		response.setResetFreeTime(module.refreshTimesFreeTime());
		response.setResetTimes(module.getResetTimes());
	}



    //根据开服时间和showtime过滤商品是否展示
    public Collection<ShopMessage.Goods> checkShopItemIsShow(String playerCode) {
        Collection<ShopMessage.Goods> goods = new StrategyShopHandle().goods(playerCode);

        try {
            //商品表信息
            Map<String, magicshop> datas = DataCenter.getAllData(magicshop.class);
            ArrayList<String> idList = new ArrayList<>();
            //个人的商品信息
            //根据区id获取开服信息,计算已开服日期
            Long alreadlyPassTime = FunctionHandle.getAlreadlyPassTime(playerCode);
            //获取不展示的商品id集合
            Set<Map.Entry<String, magicshop>> entries = datas.entrySet();
            for (Map.Entry<String, magicshop> entry : entries) {
                int showtime = entry.getValue().getShowtime();
                //showtime不等于0且  已开服时间大于showtime才展示商品
                if (alreadlyPassTime - showtime < 0 || showtime == -1) {
                    idList.add(entry.getKey());
                }
            }

            //移除对应的不展示商品
            Iterator<ShopMessage.Goods> iterator = goods.iterator();
            while (iterator.hasNext()) {
                ShopMessage.Goods good = iterator.next();
                int id = good.getId();
                if(idList.contains(String.valueOf(id))){
                    iterator.remove();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return goods;
    }


	public Result buy(String playerCode, int id, int num, ShopMessage.StrategyShopBuyResponse.Builder response) {
		BuyResponse buyResponse = new BuyResponse();
		Result result = buy(playerCode, id, num, buyResponse);
		if (result.equals(Result.Success)) {
			response.setPurchase(buyResponse.getPurchase());
			response.setGoods(buyResponse.getGoods());
		}
		return result;
	}

	@Override
	public StrategyShopModule getModule(String playerCode) {
		StrategyShopModule module = StrategyShopModule.get(playerCode);
		DateTime now = DateTime.now();
		boolean needSet = false;
		for (Goods goods : module.getGoods()) {
			needSet |= update(goods, now);
		}
		if (needSet) {
			module.set();
		}
		return module;
	}

	@Override
	public GoodsLimitType limitType(magicshop data) {
		return GoodsLimitType.forNumber(data.getLimittype());
	}

	@Override
	public int limit(magicshop data) {
		return data.getLimit();
	}

	@Override
	public int rebate(magicshop data) {
		return data.getReduce();
	}

	@Override
	public Currency cost(magicshop data, int num) {
		return Currency.create(data.getCosttype(), data.getCostid(), data.getCostnum() * num);
	}

	@Override
	public Currency purchase(magicshop data, int num) {
		return Currency.create(data.getShoptype(), data.getShopid(), data.getShopnum() * num);
	}

	@Override
	public Class<magicshop> clazz() {
		return magicshop.class;
	}

}